rm(list=ls(all=TRUE))
options(scipen=999)
####
#devtools::install_github("r-spatial/lwgeom", force = TRUE)
#install.packages('tmap')
## load packages
library(readstata13)
library(sp) # basic spatial object handling
library(raster) # raster (pixel) object handling
library(rgdal) # shapefile access
library(rgeos) # geometry operations
library(ggmap)
library(plyr)
library(dplyr)
library(lwgeom)
library(tmap)
library(ggplot2)
library(tmaptools)
library(xtable)
cshapes <- readRDS("/Users/erollkuhn/Dropbox (Personal)/Refugee Context Project/Clean Code V6/22.Map/Shape Files/gadm36_DEU_2_sp.rds")
#table(cshapes$NAME_2)
#table(cshapes$CC_2)
cshapes$FIPS <- cshapes$CC_2
### clean up FIPS
cshapes$FIPS <- mapvalues(cshapes$FIPS, from=c("01001", "01002", "01003", "01004", "01051", "01053", "01054", "01055", "01056",
"01057", "01058", "01059", "01060", "01061", "01062", "02000", "03101", "03102",
"03103", "03151", "03152", "03153", "03154", "03155", "03156", "03157", "03158",
"03241", "03251", "03252", "03254", "03255", "03256", "03257", "03351", "03352",
"03353", "03354", "03355", "03356", "03357", "03358", "03359", "03360",  "03361",
"03401","03402","03403","03404", "03405", "03451","03452", "03453", "03454", "03455",
"03456", "03457", "03458", "03459","03460", "03461","03462","04011","04012", "05111",
"05112", "05113",  "05114","05116", "05117","05119", "05120","05122","05124","05154",
"05158", "05162", "05166", "05170", "05314","05315","05316", "05334","05358","05362",
"05366", "05370","05374", "05378","05382", "05512","05513", "05515", "05554", "05558",
"05562", "05566","05570", "05711","05754", "05758", "05762", "05766",  "05770","05774",
"05911", "05913","05914", "05915", "05916", "05954", "05958", "05962", "05966", "05970", "05974",
"05978", "06411", "06412", "06413", "06414", "06431", "06432", "06433", "06434", "06435", "06436",
"06437", "06438", "06439", "06440", "06531", "06532", "06533", "06534", "06535", "06611", "06631",
"06632", "06633", "06634", "06635", "06636", "07111", "07131", "07132", "07133", "07134", "07135", "07137",
"07138", "07140", "07141", "07143", "07211", "07231", "07232", "07233", "07235", "07311", "07312", "07313",
"07314", "07315", "07316", "07317", "07318", "07319", "07320", "07331", "07332", "07333", "07334", "07335", "07336",
"07337", "07338", "07339", "07340", "08111", "08115", "08116", "08117", "08118", "08119", "08121", "08125",
"08126", "08127", "08128", "08135", "08136", "08211", "08212", "08215", "08216", "08221", "08222",
"08225", "08226", "08231", "08235", "08236", "08237", "08311", "08315", "08316", "08317", "08325", "08326",
"08327", "08335", "08336", "08337", "08415", "08416", "08417", "08421", "08425", "08426",
"08435", "08436", "08437", "09161", "09162", "09163", "09171", "09172", "09173", "09174", "09175", "09176",
"09177", "09178", "09179", "09180", "09181", "09182", "09183", "09184", "09185", "09186",
"09187", "09188", "09189", "09190", "09261", "09262", "09263", "09271", "09272", "09273", "09274",
"09275", "09276", "09277", "09278", "09279", "09361", "09362", "09363", "09371", "09372", "09373",
"09374", "09375", "09376", "09377", "09461", "09462", "09463", "09464", "09471", "09472", "09473", "09474",
"09475", "09476", "09477", "09478", "09479", "09561", "09562", "09563", "09564", "09565",
"09571", "09572", "09573", "09574", "09575", "09576", "09577", "09661", "09662", "09663", "09671", "09672", "09673",
"09674", "09675", "09676", "09677", "09678", "09679", "09761", "09762", "09763",
"09764", "09771", "09772", "09773", "09774", "09775", "09776", "09777", "09778", "09779", "09780"),
to=c("1001","1002", "1003","1004","1051","1053", "1054", "1055","1056","1057","1058","1059","1060",
"1061","1062","2000","3101","3102","3103","3151","3152","3153","3154","3155","3156","3157",
"3158","3241","3251","3252","3254","3255","3256","3257", "3351","3352","3353","3354","3355",
"3356","3357", "3358","3359","3360","3361","3401","3402","3403","3404","3405","3451","3452",
"3453","3454","3455","3456","3457","3458","3459","3460","3461","3462","4011","4012","5111",
"5112", "5113","5114","5116","5117","5119","5120","5122","5124","5154","5158","5162","5166",
"5170","5314","5315","5316","5334","5358","5362","5366","5370","5374","5378","5382","5512",
"5513", "5515","5554","5558","5562","5566","5570","5711","5754","5758","5762","5766","5770",
"5774","5911","5913","5914", "5915", "5916", "5954", "5958", "5962", "5966", "5970", "5974",
"5978", "6411", "6412","6413", "6414", "6431", "6432", "6433", "6434", "6435", "6436",
"6437", "6438", "6439", "6440", "6531", "6532", "6533", "6534", "6535", "6611", "6631",
"6632", "6633", "6634", "6635", "6636", "7111", "7131", "7132", "7133", "7134", "7135",
"7137", "7138", "7140", "7141", "7143", "7211", "7231", "7232", "7233", "7235", "7311",
"7312", "7313", "7314", "7315", "7316", "7317", "7318", "7319", "7320", "7331", "7332",
"7333", "7334", "7335", "7336", "7337", "7338", "7339", "7340", "8111", "8115", "8116",
"8117", "8118", "8119", "8121", "8125", "8126", "8127", "8128", "8135", "8136", "8211", "8212",
"8215", "8216", "8221", "8222", "8225", "8226", "8231", "8235", "8236", "8237", "8311",
"8315", "8316", "8317", "8325", "8326", "8327", "8335", "8336", "8337", "8415", "8416",
"8417", "8421", "8425", "8426", "8435", "8436", "8437", "9161", "9162", "9163", "9171", "9172",
"9173", "9174", "9175", "9176", "9177", "9178", "9179", "9180", "9181", "9182", "9183",
"9184", "9185", "9186", "9187", "9188", "9189", "9190", "9261", "9262", "9263", "9271",
"9272", "9273", "9274", "9275", "9276", "9277", "9278", "9279", "9361", "9362", "9363",
"9371", "9372", "9373", "9374", "9375", "9376", "9377", "9461", "9462", "9463", "9464",
"9471", "9472", "9473", "9474", "9475", "9476", "9477", "9478", "9479", "9561", "9562",
"9563", "9564", "9565", "9571", "9572", "9573", "9574", "9575", "9576", "9577", "9661",
"9662", "9663", "9671", "9672", "9673", "9674", "9675", "9676", "9677", "9678", "9679",
"9761", "9762", "9763", "9764", "9771", "9772", "9773", "9774", "9775", "9776", "9777",
"09778", "9779", "9780"))
#table(cshapes$NAME_1)
##########################
### call the county data
setwd("/Users/erollkuhn/Dropbox (Personal)/Refugee Context Project/Clean Code V6/22.Map")
df <- read.csv("CountyStats_V2.csv")
df$foreignborn <- as.numeric(df$foreignborn)
cshapes@data <- join(cshapes@data, df,by=c("FIPS"))
map_foreignborn <- tm_shape(cshapes) +
tm_borders(lwd=0.1) +
tm_fill(col = "foreignborn",
colorNA="grey",
title="% Foreign-Born",
style = "cont",
palette=get_brewer_pal("YlGnBu", n = 10)) +
tm_layout(legend.outside = TRUE)
map_nonEU <- tm_shape(cshapes) +
tm_borders(lwd=0.1) +
tm_fill(col = "nonEU",
colorNA="grey",
title="% non-EU Foreign-Born",
style = "cont",
palette=get_brewer_pal("YlGnBu", n = 10)) +
tm_layout(legend.outside = TRUE)
map_welcome <- tm_shape(cshapes) +
tm_borders(lwd=0.1) +
tm_fill(col = "welcome",
colorNA="grey",
title="Mean Welcomeness Index",
style = "cont",
palette=get_brewer_pal("PuBuGn", n = 10)) +
tm_layout(legend.outside = TRUE)
setwd("/Users/erollkuhn/Dropbox (Personal)/Refugee Context Project/Clean Code V6/22.Map/Output")
pdf("map_foreignborn.pdf",width=6,height=4)
map_foreignborn
dev.off()
pdf("map_nonEU.pdf",width=6,height=4)
map_nonEU
dev.off()
pdf("map_welcome.pdf",width=6,height=4)
map_welcome
dev.off()
### tables
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
aggregate(x = df$statename, by = list(df$abovemedian_fb), FUN = "sum")
df$statename
df$abovemedian_fb
list(df$abovemedian_fb)
aggregate(x = df$statename, by = list(df$abovemedian_fb), FUN = "mean")
aggregate(x = df$statename, by = list(df$abovemedian_fb), FUN = "sum")
aggregate(x = df$one, by = list(df$abovemedian_fb), FUN = "sum")
df$one <- 1
aggregate(x = df$one, by = list(df$abovemedian_fb), FUN = "sum")
aggregate(x = df$one, by = list(df$abovemedian_fb,df$statename), FUN = "sum")
aggregate(x = df$one, by = list(df$abovemedian_fb*df$statename), FUN = "sum")
aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
df$one <- 1
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
above_median_count
length(above_median_count)
sum(above_median_count[1:length(above_median_count[1,]),])
length(above_median_count[1,])
length(above_median_count[,1])
above_median_count[1:length(above_median_count[,1]),]
1:length(above_median_count[,1])
above_median_count[1:15,]
above_median_count[1:15,2]
sum(above_median_count[1:15,2])
total <- sum(above_median_count[1:15,2])
above_median_count[,3] <- (above_median_count[,2]/total)*100
above_median_count
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum") # counties above median
df$one <- 1
numb_count <- aggregate(x = df$one, by = list(df$statename), FUN = "sum")
above_median_count[,3] <- numb_count
total <- sum(above_median_count[1:15,2])
above_median_count[,4] <- (above_median_count[,2]/total)*100
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum") # counties above median
df$one <- 1
numb_count <- aggregate(x = df$one, by = list(df$statename), FUN = "sum")
numb_count
above_median_count[,3] <- numb_count
above_median_count
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum") # counties above median
df$one <- 1
numb_count <- aggregate(x = df$one, by = list(df$statename), FUN = "sum")
above_median_count[,3] <- numb_count[,2]
above_median_count
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum") # counties above median
df$one <- 1
numb_count <- aggregate(x = df$one, by = list(df$statename), FUN = "sum")
total <- sum(above_median_count[1:15,2])
above_median_count[,4] <- (above_median_count[,2]/total)*100
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
df$abovemedian_fb
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum") # counties above median
above_median_count
numb_count <- aggregate(x = df$one, by = list(df$statename), FUN = "sum")
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
above_median_count[,3] <-
total <- sum(above_median_count[1:15,2])
above_median_count[,4] <- (above_median_count[,2]/total)*100
above_median_count
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
above_median_count[,3] <-
total <- sum(above_median_count[1:15,2])
above_median_count[,4] <- (above_median_count[,2]/total)*100
x <- cbind(above_median_count[,1],round(above_median_count[,4],2))
x
df$abovemedian_nonEU <- ifelse(df$nonEU>median(df$nonEU),1,0)
df$abovemedian_nonEU
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
above_median_count
above_median_count[,3] <-
total <- sum(above_median_count[1:15,2])
total
total <- sum(abovemedian_nonEU[1:15,2])
total <- sum(df$abovemedian_nonEU[1:15,2])
df$abovemedian_nonEU <- ifelse(df$nonEU>median(df$nonEU),1,0)
total <- sum(abovemedian_nonEU[1:15,2])
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
total <- sum(above_median_count[1:15,2])
above_median_count[,4] <- (above_median_count[,2]/total)*100
x <- cbind(above_median_count[,1],round(above_median_count[,4],2))
df$abovemedian_nonEU <- ifelse(df$nonEU>median(df$nonEU),1,0)
total <- sum(df$abovemedian_nonEU[1:15,2])
abovemedian_nonEU <- ifelse(df$nonEU>median(df$nonEU),1,0)
abovemedian_nonEU
total <- sum(abovemedian_nonEU[1:15,2])
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
total <- sum(above_median_count[1:15,2])
above_median_count[,4] <- (above_median_count[,2]/total)*100
x <- cbind(above_median_count[,1],round(above_median_count[,4],2))
df$abovemedian_nonEU <- ifelse(df$nonEU>median(df$nonEU),1,0)
total <- sum(df$abovemedian_nonEU[1:15,2])
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
table(df$abovemedian_fb,df$statename)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
total <- sum(above_median_count[1:15,2])
above_median_count
above_median_count[,3] <- (above_median_count[,2]/total)*100
x <- cbind(above_median_count[,1],round(above_median_count[,4],2))
x <- cbind(above_median_count[,1],round(above_median_count[,3],2))
x
ifelse(df$nonEU>median(df$nonEU),1,0)
df$abovemedian_nonEU <- ifelse(df$nonEU>median(df$nonEU),1,0)
df$abovemedian_nonEU
(abovemedian_nonEU[,2]/total)*100
(abovemedian_nonEU/total)*100
above_median_count_nonEU <- (df$abovemedian_nonEU/total)*100
above_median_count_nonEU
above_median_count[,1]
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
df$abovemedian_nonEU <- ifelse(df$nonEU>median(df$nonEU),1,0)
above_median_count_nonEU <- aggregate(x = df$abovemedian_nonEU, by = list(df$statename), FUN = "sum")
total <- sum(above_median_count[1:15,2])
above_median_count[,3] <- (above_median_count[,2]/total)*100
above_median_count[,4] <- (above_median_count_nonEU[,2]/total)*100
x <- cbind(above_median_count[,1],
round(above_median_count[,3],2),
round(above_median_count[,4],2))
colnames(x) <- c("Federal State",
"Percent of Counties Above Median of Foreign-Born",
"Percent of Counties Above Median of non-EU Foreign-Born")
x
colnames(x) <- c("Federal State",
"Percent of Counties Above /n Median of Foreign-Born",
"Percent of Counties Above /n Median of non-EU Foreign-Born")
x
df$abovemedian_fb <- ifelse(df$foreignborn>median(df$foreignborn),1,0)
above_median_count <- aggregate(x = df$abovemedian_fb, by = list(df$statename), FUN = "sum")
df$abovemedian_nonEU <- ifelse(df$nonEU>median(df$nonEU),1,0)
above_median_count_nonEU <- aggregate(x = df$abovemedian_nonEU, by = list(df$statename), FUN = "sum")
total <- sum(above_median_count[1:15,2])
above_median_count[,3] <- (above_median_count[,2]/total)*100
above_median_count[,4] <- (above_median_count_nonEU[,2]/total)*100
x <- cbind(above_median_count[,1],
round(above_median_count[,3],2),
round(above_median_count[,4],2))
colnames(x) <- c("Federal State",
"% Foreign-Born",
"% non-EU Foreign-Born")
print(xtable(x,
caption="Proportion of All Counties Above the Median Value of Demographic Context, by Federal State"),
include.rownames=FALSE)
x
xb <- x
xb <- xb[c(3,10),]
xb
xb[3,2] <- 100-(xb[2,2] + xb[1,2])
xb[1,2
xb[1,2]
as.numeric(xb[1,2])
100-(as.numeric(xb[2,2]) + as.numeric(xb[1,2]))
xb <- x
xb <- xb[c(3,10),]
xb[3,1] <- "All Other States"
xb[3,2] <- 100-(as.numeric(xb[2,2]) + as.numeric(xb[1,2]))
xb[3,3] <- 100-(as.numeric(xb[2,3]) + as.numeric(xb[1,3]))
xb <- x
xb <- xb[c(3,10),]
xb[3,1] <- "All Other States"
xb <- x
xb <- xb[c(3,10),]
xb
xb[3,1]
xb <- x
xb <- xb[c(3,10),]
xb[3,1:3] <- NA
xb <- x
xb <- xb[c(3,10),]
xb[3,] <- NA
c("All Other States",
(100-(as.numeric(xb[2,2]) + as.numeric(xb[1,2]))),
(100-(as.numeric(xb[2,3]) + as.numeric(xb[1,3]))))
xb <- x
xb <- xb[c(3,10),]
three <- c("All Other States",
(100-(as.numeric(xb[2,2]) + as.numeric(xb[1,2]))),
(100-(as.numeric(xb[2,3]) + as.numeric(xb[1,3]))))
xbc <- rbind(xb,three)
xbc
print(xtable(x,
caption="Proportion of All Counties Above the Median Value of Demographic Context, by Federal State"),
include.rownames=FALSE)
xb <- x
xb <- xb[c(3,10),]
three <- c("All Other States",
(100-(as.numeric(xb[2,2]) + as.numeric(xb[1,2]))),
(100-(as.numeric(xb[2,3]) + as.numeric(xb[1,3]))))
xbc <- rbind(xb,three)
print(xtable(xbc,
caption="Proportion of All Counties Above the Median Value of Demographic Context, by Federal State"),
include.rownames=FALSE)
print(xtable(x,
caption="Proportion of All Counties Above the Median Value of Demographic Context, by Federal State"),
include.rownames=FALSE)
## specific table
xb <- x
xb <- xb[c(3,10),]
three <- c("All Other States",
(100-(as.numeric(xb[2,2]) + as.numeric(xb[1,2]))),
(100-(as.numeric(xb[2,3]) + as.numeric(xb[1,3]))))
xbc <- rbind(xb,three)
print(xtable(xbc,
caption="Proportion of All Counties Above the Median Value of Demographic Context, by Federal State"),
include.rownames=FALSE)
